﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Boom.Navigation;

namespace Boom.GameObjects
{
    public class ListBrick:DrawableGameComponent
    {
        private List<Brick> _List;
        private List<Texture2D> _TextureDes;
        private Texture2D _TextureNonDes;
        private List<int> _PositionBrick;
        private int _TimeBoom;
        private TimeSpan _ElapsedTimeDes;

        public List<Brick> List
        {
            get { return _List; }
            set { _List = value; }
        }

        public List<int> PositionBrick
        {
            get { return _PositionBrick; }
            set { _PositionBrick = value; }
        }

        public ListBrick(Game Mygame, ref List<Texture2D> TextureDes, ref Texture2D TextureNonDes, List<Vector2> PosDes,List<int> TypeDes, List<Vector2> PosNonDes, List<int> TypeNonDes)
            : base(Mygame)
        {
            _TextureDes = TextureDes;
            _TextureNonDes = TextureNonDes;
            _List = new List<Brick>();
            _PositionBrick = new List<int>();
            _ElapsedTimeDes = TimeSpan.Zero;
            _TimeBoom = Config.TimeBoom;

            Create_DesBrick(PosDes,TypeDes, PosNonDes, TypeNonDes);
            PositionBrick.Sort();
        }

        //Khởi tạo gạch từ file bản đồ
        private void Create_DesBrick(List<Vector2> PosDes,List<int> TypeDes, List<Vector2> PosNonDes, List<int> TypeNonDes)
        {
            //Khởi tạo các đối tượng gạch có thể phá vỡ
            for (int i = 0; i < PosDes.Count; i++)
            {
                Texture2D TextureDes = _TextureDes[TypeDes[i] - Config.MinTypeDesBrick];
                List.Add(new Brick(Game, ref TextureDes, PosDes[i], true));
                PositionBrick.Add((int)(15 * PosDes[i].Y / 40 + PosDes[i].X / 40));
            }

            //Khởi tạo các đối tượng gạch không thể phá vỡ
            for (int j = 0; j < PosNonDes.Count; j++)
            {
                Brick newbrick = new Brick(Game, ref _TextureNonDes, PosNonDes[j], false);
                newbrick.ActiveFrame = TypeNonDes[j] - Config.MinTypeNonDesBrick;
                List.Add(newbrick);
                PositionBrick.Add((int)(15 * PosNonDes[j].Y / 40 + PosNonDes[j].X / 40));
            }
        }

        public override void Initialize()
        {
            for(int i=0;i< List.Count;i++)
            {
                List[i].Initialize();
            }
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < List.Count; i++)
            {
                List[i].Update(gameTime);
              
                //Xóa đối tượng viên gạch trên màn hình
                if (!List[i].Enabled)
                {
                    List.RemoveAt(i);
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _PositionBrick.Sort();
            for (int i = 0; i < List.Count; i++)
            {
                List[i].Draw(gameTime);
            }
            base.Draw(gameTime);
        }
    }
}
